<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
<head>
    <title>反馈管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/all.min.css">
    <style>
        .container {
            padding: 20px;
        }
        .top-bar {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .search-box {
            display: flex;
            align-items: center;
        }
        .search-box input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            width: 200px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
        }
        .btn-primary {
            background-color: #1ab394;
        }
        .btn-primary:hover {
            background-color: #18a689;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e7eaec;
        }
        th {
            background-color: #f5f5f6;
            color: #333;
        }
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
        }
        .pagination button {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ddd;
            background-color: white;
            cursor: pointer;
            border-radius: 3px;
        }
        .pagination button:hover:not(:disabled) {
            background-color: #f5f5f5;
            color: #1ab394;
        }
        .pagination button.active {
            background-color: #1ab394;
            color: white;
            border-color: #1ab394;
        }
        .pagination button:disabled {
            background-color: #f5f5f5;
            cursor: not-allowed;
        }
        .pagination-info {
            margin: 0 15px;
            color: #666;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        .modal-content {
            position: relative;
            background-color: #fff;
            margin: 100px auto;
            padding: 20px;
            width: 60%;
            max-width: 800px;
            border-radius: 5px;
        }
        .close {
            position: absolute;
            right: 20px;
            top: 10px;
            font-size: 24px;
            cursor: pointer;
        }
        .feedback-detail {
            margin-top: 20px;
        }
        .feedback-detail h3 {
            margin-bottom: 15px;
            color: #333;
        }
        .feedback-detail p {
            margin-bottom: 10px;
            line-height: 1.6;
        }
        .feedback-meta {
            color: #666;
            font-size: 14px;
            margin-bottom: 15px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="top-bar">
            <h2><i class="fas fa-comment-dots"></i> 反馈管理</h2>
            <div class="search-box">
                <input type="text" id="searchInput" placeholder="输入标题搜索" value="${title}">
                <button class="btn btn-primary" onclick="search()">
                    <i class="fas fa-search"></i> 搜索
                </button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>反馈人</th>
                    <th>提交时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${feedbackList}" var="feedback">
                    <tr>
                        <td>${feedback.feedbackId}</td>
                        <td>${feedback.title}</td>
                        <td>${feedback.username}</td>
                        <td><fmt:formatDate value="${feedback.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>

                        <td>
                            <button class="btn btn-primary" onclick="viewFeedback(${feedback.feedbackId})">
                                <i class="fas fa-eye"></i> 查看
                            </button>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>

        <div class="pagination">
            <button onclick="goToPage(${currentPage - 1})" ${currentPage <= 1 ? 'disabled' : ''}>
                <i class="fas fa-chevron-left"></i> 上一页
            </button>
            
            <c:forEach begin="1" end="${totalPages}" var="i">
                <c:choose>
                    <c:when test="${i == currentPage}">
                        <button class="active">${i}</button>
                    </c:when>
                    <c:otherwise>
                        <button onclick="goToPage(${i})">${i}</button>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            
            <button onclick="goToPage(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
                下一页 <i class="fas fa-chevron-right"></i>
            </button>
            
            <span class="pagination-info">
                共 ${totalCount} 条记录，${totalPages} 页
            </span>
        </div>
    </div>

    <!-- 反馈详情模态框 -->
    <div id="feedbackModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal()">&times;</span>
            <div id="feedbackDetail" class="feedback-detail">
                <!-- 反馈详情将通过JavaScript动态插入 -->
            </div>
        </div>
    </div>

    <script>
        function formatDate1(timestamp) {
            const date = new Date(Number(timestamp));
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            const seconds = String(date.getSeconds()).padStart(2, '0');
            return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
        }

        function search() {
            const searchValue = document.getElementById('searchInput').value.trim();
            location.href = '${pageContext.request.contextPath}/admin/feedback/list?title=' + 
                          encodeURIComponent(searchValue);
        }

        document.getElementById('searchInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                search();
            }
        });

        function goToPage(page) {
            const searchValue = document.getElementById('searchInput').value.trim();
            const searchParam = searchValue ? '&title=' + encodeURIComponent(searchValue) : '';
            location.href = '${pageContext.request.contextPath}/admin/feedback/list?page=' + page + searchParam;
        }

        function viewFeedback(feedbackId) {
            fetch('${pageContext.request.contextPath}/admin/feedback/detail?id=' + feedbackId)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        const feedback = data.data;
                       
                        const formattedDate = formatDate1(feedback.createTime);
                       
                        
                        document.getElementById('feedbackDetail').innerHTML = `
                            <h3>\${feedback.title}</h3>
                            <div class="feedback-meta">
                                <span>反馈人：\${feedback.username}</span>
                                <span style="margin-left: 20px">提交时间：\${formattedDate}</span>
                            </div>
                            <p>\${feedback.content}</p>
                        `;
                        document.getElementById('feedbackModal').style.display = 'block';
                    } else {
                        alert(data.message || '获取反馈详情失败');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('获取反馈详情失败');
                });
        }

        function closeModal() {
            document.getElementById('feedbackModal').style.display = 'none';
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('feedbackModal');
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    </script>
</body>
</html>